package com.lee.dhsg.dao;

import com.lee.dhsg.dao.provider.OrderSqlProvider;
import com.lee.dhsg.domain.Order;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author Jun
 */
@Component
@Mapper
public interface OrderDao {
    /**
     * 根据id查订单
     * @param oid
     * @return Order
     */
    @Select("select * from `order`as o, course as c where o.courseid=c.courseid and sid =#{sid}")
    @Results({
            @Result(id=true,column = "oid",property = "oid"),
            @Result(column = "price",property = "price"),
            @Result(column = "time",property = "time"),
            @Result(column = "courseid",property = "course",
            one = @One(
                    select ="com.dhsg.dao.CourseDao.selectById",
                    fetchType = FetchType.EAGER
            ))
    })
    Order selectByOid(Integer oid);
    /**
     * 动态查询订单数量
     * @param params
     * @return
     */
    @SelectProvider(type = OrderSqlProvider.class,method = "count")
    Integer countOrder(Map<String, Object> params);

    /**
     *动态查询订单
     * @param params
     * @return List<Order>
     */
    @SelectProvider(type = OrderSqlProvider.class,method = "selectWhitParam")
    @Results({
            @Result(id=true,column = "oid",property = "oid"),
            @Result(column = "price",property = "price"),
            @Result(column = "time",property = "time"),
            @Result(column = "sid",property = "student",
                    one=@One(
                            select = "com.dhsg.dao.StudentDao.selectById",
                            fetchType = FetchType.EAGER
                    )),
            @Result(column = "courseid",property = "course",
                    one=@One(
                            select = "com.dhsg.dao.CourseDao.selectById",
                            fetchType = FetchType.EAGER
                    ))
    })
    List<Order> selectByPage(Map<String, Object> params);

    /**
     * 生成订单
     * @param params
     * @return
     */
    @SelectProvider(type = OrderSqlProvider.class,method = "insertOrder")
    @Options(useGeneratedKeys = true,keyProperty = "oid")
    void saveOrder(Map<String, Object> params);

    /**
     * 根据id删除订单
     * @param oid
     */
    @Delete("delete from `order` where oid=#{oid}")
    void deleteByOid(Integer oid);
}
